﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.Data;
using Xinkui.WebModel.Cache;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel;
using Xinkui.WebModel.Model;

public partial class UserControl_fore_UC_Catalog_Product : Xinkui.WebModel.BasicControl.ForeControl {

    public string RootName { get; set; }
    public string RootDescr { get; set; }



    protected int _pageSIze = 8;
    public int PageSIze {
        get {
            return _pageSIze;
        }
        set {
            if (_pageSIze == value)
                return;
            _pageSIze = value;
        }
    }

    public string ProductListURL { get; set; }

    public string CompanyListURL { get; set; }

    public SearchQueryData Query { get; set; }

    protected void Page_Load(object sender, EventArgs e) {
        Query = SearchQueryData.Create();


        var rootCategory = CategoryCache.ListAllCategory().FirstOrDefault(u => u.CategoryInfo.CategoryID == Query.CategoryLevel1);
        this.BasePage.Enqueue(NavItem.Create("Product"));
        if (rootCategory != null) {
            RootName = rootCategory.CategoryInfo.Title;
            RootDescr = rootCategory.CategoryInfo.Description;
            if (Query.CategoryLevel2 > 0) {
                this.BasePage.Enqueue(NavItem.Create(RootName, URLHelper.LinkToCatalogProducts(Query.CategoryLevel1, 0, 0)));
            }
            else {
                this.BasePage.Enqueue(NavItem.Create(RootName));
            }
        }
        if (Query.CategoryLevel2 > 0) {
            if (Query.CategoryLevel3 > 0) {
                this.BasePage.Enqueue(NavItem.Create(rootCategory.SubViews.Where(u => u.CategoryInfo.CategoryID == Query.CategoryLevel2).Select(u => u.CategoryInfo.Title).FirstOrDefault(), URLHelper.LinkToCatalogProducts(Query.CategoryLevel1, Query.CategoryLevel2, 0)));
            }
            else {
                this.BasePage.Enqueue(NavItem.Create(rootCategory.SubViews.Where(u => u.CategoryInfo.CategoryID == Query.CategoryLevel2).Select(u => u.CategoryInfo.Title).FirstOrDefault()));
            }
        }
        if (Query.CategoryLevel3 > 0) {
            Xinkui.WebModel.Cache.View.CategoryView rootCategorySubViewsWhere = rootCategory.SubViews.FirstOrDefault(u => u.CategoryInfo.CategoryID == Query.CategoryLevel2);
            if (rootCategorySubViewsWhere != null) {
                this.BasePage.Enqueue(NavItem.Create(rootCategorySubViewsWhere.SubViews.Where(u => u.CategoryInfo.CategoryID == Query.CategoryLevel3).Select(u => u.CategoryInfo.Title).FirstOrDefault()));
            }
        }
        
        int total = 0;
        var listSource = Singleton<ProductInfoDataAccess>.Instance.ListProductsByCategory(Query.PageIndex, PageSIze, Query.CategoryLevel3 > 0 ? Query.CategoryLevel3 : (Query.CategoryLevel2 > 0 ? Query.CategoryLevel2 : Query.CategoryLevel1), out total);

        this.rptProductSource.DataSource = listSource;
        this.rptProductSource.DataBind();


        UC_RewritePager1.FormatUrl = true;
        UC_RewritePager1.CoverNav = false;

        this.UC_RewritePager1.NoteTotal = total;
        this.UC_RewritePager1.JumpUrl = URLHelper.LinkToCatalogProducts(Query.CategoryLevel1, Query.CategoryLevel2, Query.CategoryLevel3) + "&p={0}";//"ProductListForImage.aspx?com=1&p={0}";//JumpUrl;
        this.UC_RewritePager1.PageIndex = Query.PageIndex;
        this.UC_RewritePager1.PageSize = PageSIze;
    }




    public string BindingProductProperty(string title, object o) {
        if (o != null && !string.IsNullOrEmpty(o.ToString())) {
            return string.Format(@" <div class=""para""><strong>{1}:</strong>{0}</div>", o, title);
        }
        return string.Empty;
    }
}